package com.tj.collection.controller.lending.response;

import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.EntityUtils;
import com.tj.collection.db.entity.ClMerchantEntity;
import com.tj.collection.db.entity.business.ClAllocationInfoEntity;
import com.tj.collection.utils.CalculatorUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/9/10
 * Time: 下午5:57
 */
public class ClQueryExpResponse implements ResponseData {

    private List<String[]> itemList;

    public ClQueryExpResponse(List<ClAllocationInfoEntity> clAllocationInfoEntityList, List<ClMerchantEntity> clMerchantEntityList) {
        Map<String, ClMerchantEntity> clMerchantEntityMap = EntityUtils.makeEntityMap(clMerchantEntityList, "merchantCode");

        String[] row1 = {"Merchant", "Product", "Loan ID", "Collection No", "Mobile", "Name",
                "Last call", "Agent Name", "Admin ID", "Planned Repayment Date", "Borrow Capital",
                "Total Amount Should Repay", "Actual Repayment Time", "Repaid Amount"
        };
        this.itemList = new ArrayList<>();
        this.itemList.add(row1);

        for (ClAllocationInfoEntity entity : clAllocationInfoEntityList) {
            BigDecimal expireAmount = null;
            BigDecimal lateFee = null;
            if (entity.getOverdueDays() > 0) {
                lateFee = CalculatorUtils.calculateOverdueFee(entity.getBorrowCapital(),
                        entity.getLateFeeRatio(),
                        entity.getOverdueDays()).add(entity.getLateServiceFee());
                //本金+利息+罚息
                expireAmount = entity.getBorrowCapital().add(entity.getInterest()).add(lateFee);
            } else {
                expireAmount = entity.getBorrowCapital().add(entity.getInterest());
            }
            expireAmount = expireAmount.setScale(2, BigDecimal.ROUND_HALF_UP);

            String[] row = new String[row1.length];
            row[0] = entity.getSourceMerchantCode();
            row[1] = clMerchantEntityMap.containsKey(entity.getSourceMerchantCode()) ? clMerchantEntityMap.get(entity.getSourceMerchantCode()).getAppName() : entity.getSourceMerchantCode();
            row[2] = entity.getOrderNo();
            row[3] = entity.getCollectionNo();
            row[4] = entity.getMobile();
            row[5] = entity.getName();

            row[6] = entity.getLastHandleTime() == null ? "" : DateUtil.getDate(entity.getLastHandleTime(), "yyyy-MM-dd HH:mm:ss");
            row[7] = entity.getAgentName();
            row[8] = entity.getAdminId() + "";
            row[9] = DateUtil.getDate(entity.getRepaymentDate(), "yyyy-MM-dd HH:mm:ss");
            row[10] = entity.getBorrowCapital().setScale(2, BigDecimal.ROUND_HALF_UP).toString();

            row[11] = expireAmount.toString();
            row[12] = entity.getActualRepaymentDate() == null ? "" : DateUtil.getDate(entity.getActualRepaymentDate(), "yyyy-MM-dd HH:mm:ss");
            row[13] = entity.getPaidAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            this.itemList.add(row);
        }
    }
}
